/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces;

import goodenglish.application.LexiconEditService;
import goodenglish.application.impl.LexiconEditServiceImpl;
import goodenglish.domain.model.lexicon.Lexicon;
import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.model.subt.SubtRepository;
import goodenglish.domain.service.Translator;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.repository.db.SubtRepositoryDb;
import goodenglish.interfaces.component.MyExplainPane;
import goodenglish.interfaces.javafxex.MyTextArea;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 设置词义界面
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class NewWordExplainSetPane extends BorderPane {

	private static Log logger = LogFactory.getLog(NewWordExplainSetPane.class);
	private Lexicon lexicon;
	// 单词拼写
	Button wordSpellViewBtn;

	private String emptyWordStr = "暂没有单词";
	// 当前显示单词的下标
	private int index = 0;
	private LexiconChoiceBox subtChoiceBox;
	private MyExplainPane explainPane1;
	private MyTextArea wordSpellEditArea;
	private MyTextArea explainEditArea;
	private MyExplainPane explainPane2;
	private WebView browser;
	FlowPane buttonsPane;

	public NewWordExplainSetPane() {
		BorderPane.setAlignment(this, Pos.TOP_LEFT);
		this.setCenter(getCenterPane());
		this.setRight(getRightPane());

	}

	private VBox getCenterPane() {

		VBox vBox1 = new VBox();

		// 添加操作按钮行
		addOprationBtnsLine(vBox1);

		// 添加单词词义行
		addExplainLine(vBox1);

		// 添加单词解释框行
		addSentenseLine(vBox1);

		// 添加例句显示框行
		addLineFour(vBox1);
		BorderPane.setAlignment(vBox1, Pos.TOP_LEFT);
		return vBox1;
	}

	private void addLineFour(VBox vBox1) {
		/**
		 * 添加下方的句子
		 */
		int centenceHeight = 300;
		int centenceWidth = 600;

		ScrollPane sentencePanel = new ScrollPane();
		sentencePanel.setMinWidth(centenceWidth);
		sentencePanel.setMaxWidth(centenceWidth);
		sentencePanel.setMinHeight(centenceHeight);
		sentencePanel.setStyle("-fx-border-color: black;-fx-border-width: 2;-fx-alignment:top-left;");
		browser = new WebView();
		browser.setPrefSize(centenceWidth, centenceHeight);
		browser.setMinWidth(centenceWidth);
		sentencePanel.setContent(browser);
		vBox1.getChildren().add(sentencePanel);
	}

	private void addExplainLine(VBox vBox1) {
		int height = 200;
		HBox hBox = new HBox();
		hBox.setMinHeight(height);
		hBox.setMaxHeight(height);
		/**
		 * 添加中间
		 */
		wordSpellViewBtn = new Button(emptyWordStr);
		wordSpellViewBtn.setWrapText(true);
		wordSpellViewBtn.setId("wordSpellViewBtn");
		wordSpellViewBtn.setMinHeight(height);
		wordSpellViewBtn.setMaxHeight(height);
		hBox.getChildren().add(wordSpellViewBtn);

		wordSpellEditArea = new MyTextArea("编辑拼写", 200, height, true);
		hBox.getChildren().add(wordSpellEditArea);

		vBox1.getChildren().add(hBox);

	}

	private void addOprationBtnsLine(VBox vBox1) {
		/**
		 * 添加顶部控件组
		 */
		FlowPane topFlowPane;
		topFlowPane = new FlowPane(20, 20);
		topFlowPane.setLayoutX(10);
		topFlowPane.setLayoutY(Config.MENU_HEIGHT);

		subtChoiceBox = new LexiconChoiceBox();
		topFlowPane.getChildren().add(subtChoiceBox);

		/**
		 * 添加保存认识单词按钮
		 */
		logger.info("添加提取生词按钮");
		Button btnRememberSave = new Button("提取生词");
		btnRememberSave.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				// 分析字幕
				lexicon = subtChoiceBox.getSelectLexicon();
				displayWord();
				addButtons();

			}
		});
		topFlowPane.getChildren().add(btnRememberSave);
		vBox1.getChildren().add(topFlowPane);
	}

	private VBox getRightPane() {
		/**
		 * 添加右边
		 */
		VBox vBox = new VBox();
		Button button = new Button("下一个");
		button.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent t) {
				next();

			}
		});
		button.setFont(new Font(100));
		button.setTooltip(new Tooltip("快捷键：page_down"));
		vBox.getChildren().add(button);

		Button button2 = new Button("上一个");
		button2.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent t) {
				previous();

			}
		});
		button2.setFont(new Font(100));
		button2.setTooltip(new Tooltip("快捷键：page_up"));
		vBox.getChildren().add(button2);

		this.setOnKeyPressed(new EventHandler<KeyEvent>() {

			public void handle(KeyEvent t) {

				if (t.getCode().equals(KeyCode.PAGE_DOWN)) {
					next();
				}
				if (t.getCode().equals(KeyCode.PAGE_UP)) {
					previous();
				}

			}
		});
		buttonsPane = new FlowPane(4, 4);
		buttonsPane.setMaxWidth(300);
		ScrollPane scrollPane = new ScrollPane();
		scrollPane.setContent(buttonsPane);
		scrollPane.setMaxWidth(310);
		scrollPane.setMinHeight(450);
		scrollPane.setMaxHeight(450);
		vBox.getChildren().add(scrollPane);
		return vBox;
	}

	public void addSentenseLine(VBox vBox1) {
		int height = 150;
		HBox hBox = new HBox();
		vBox1.getChildren().add(hBox);
		/**
		 * 添加解释编辑
		 */
		explainEditArea = new MyTextArea("编辑解释", 300, height, true);
		hBox.getChildren().add(explainEditArea);
		explainPane1 = new MyExplainPane("微软解释", "ms", 300, height);

		hBox.getChildren().add(explainPane1);
		/**
		 * 添加解释1
		 */
		explainPane2 = new MyExplainPane("本地解释", "local", 300, height);

		hBox.getChildren().add(explainPane2);

	}

	private void next() {
		gotoWord(index + 1);
	}

	private void previous() {
		gotoWord(index - 1);
	}

	private void gotoWord(int gotoIndex) {
		// 如果当前有单词就保存当前单词，并显示要跳转的单词单词
		if (!wordSpellViewBtn.getText().equals(emptyWordStr)) {
			NewWord word = lexicon.getNewWords().get(index);
			lexiconEditService.editNewWord(lexicon, wordSpellEditArea.getText(), explainEditArea.getText());
			index = gotoIndex;
			displayWord();
		}
	}

	public void displayWord() {
		logger.info("display word execute");
		if (lexicon.getNewWords().size() <= 0) {
			return;
		}
		if (-1 == index) {
			index = lexicon.getNewWords().size() - 1;
		}
		if (lexicon.getNewWords().size() == index) {
			index = 0;
		}

		NewWord newWord = lexicon.getNewWords().get(index);
		// 设置单词拼写显示
		wordSpellViewBtn.setText(newWord.getSpell());
		// 设置单词拼写修改
		wordSpellEditArea.setText(newWord.getSpell());
		WebVieweHelper.setHtmlText(browser, newWord.getExampleSentStr());
		explainPane1.setText(translator.englishToChiniseMs(newWord.getSpell()));

		explainPane2.setText(newWord.getLocalExplain1());
		// 设置编辑解释
		if (null == newWord.getExplain()) {
			explainEditArea.setText(translator.englishToChiniseMs(newWord.getSpell()));
		} else {
			explainEditArea.setText(newWord.getExplain());
		}

		// 放入剪切板
		Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable tText = new StringSelection(newWord.getSpell());
		clipBoard.setContents(tText, null);
		// 朗读单词
		newWord.speek();
	}

	public void addButtons() {
		buttonsPane.getChildren().clear();
		for (int i = 0; i < lexicon.getNewWords().size(); i++) {
			final int index1 = i;
			NewWord word = lexicon.getNewWords().get(i);
			Button button = new Button();
			button.setText(word.getSpell());
			if (word.getExplain() != null) {
				button.setStyle("-fx-base: green;-fx-font-size:20;");
			} else {
				button.setStyle("-fx-font-size:20;");
			}

			button.setOnAction(new EventHandler<ActionEvent>() {

				public void handle(ActionEvent t) {
					gotoWord(index1);
				}
			});
			buttonsPane.getChildren().add(button);
		}
	}

	private SubtRepository subtRepository = BeanFactory.getSingleInstance(SubtRepositoryDb.class);
	private LexiconEditService lexiconEditService = BeanFactory.getSingleInstance(LexiconEditServiceImpl.class);
	private Translator translator = BeanFactory.getSingleInstance(Translator.class);
}
